package com.shop.dao;

import com.shop.bean.TradeRecord;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class TradeDAO extends BaseDAO {

    // 创建交易记录
    public void addTradeRecord(int productId, int purchaseId) {
        try (Connection conn = getConnection();
             PreparedStatement ps = conn.prepareStatement(
                     "INSERT INTO trade_record(product_id,purchase_id,status,seller_confirm,buyer_confirm) " +
                     "VALUES(?,?,0,0,0)")) {
            ps.setInt(1, productId);
            ps.setInt(2, purchaseId);
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    // 更新交易确认状态
    public void updateTradeConfirm(int purchaseId, String role) {
        String sql = role.equals("seller") ? 
            "UPDATE trade_record SET seller_confirm=1 WHERE purchase_id=?" :
            "UPDATE trade_record SET buyer_confirm=1 WHERE purchase_id=?";
        
        try (Connection conn = getConnection();
             PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setInt(1, purchaseId);
            ps.executeUpdate();
            
            // 双方都确认则标记交易完成
            checkAndCompleteTrade(purchaseId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    // 检查并完成交易
    private void checkAndCompleteTrade(int purchaseId) throws SQLException {
        try (Connection conn = getConnection();
             PreparedStatement ps = conn.prepareStatement(
                     "SELECT * FROM trade_record WHERE purchase_id=?")) {
            ps.setInt(1, purchaseId);
            ResultSet rs = ps.executeQuery();
            if (rs.next() && rs.getInt("seller_confirm") == 1 && rs.getInt("buyer_confirm") == 1) {
                try (PreparedStatement ps2 = conn.prepareStatement(
                        "UPDATE trade_record SET status=1 WHERE purchase_id=?")) {
                    ps2.setInt(1, purchaseId);
                    ps2.executeUpdate();
                }
            }
        }
    }

    // 获取所有交易记录
    public List<TradeRecord> getAllTradeRecords() {
        List<TradeRecord> list = new ArrayList<>();
        try (Connection conn = getConnection();
             PreparedStatement ps = conn.prepareStatement("SELECT * FROM trade_record")) {
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                TradeRecord record = new TradeRecord();
                record.setId(rs.getInt("id"));
                record.setProductId(rs.getInt("product_id"));
                record.setPurchaseId(rs.getInt("purchase_id"));
                record.setStatus(rs.getInt("status"));
                record.setSellerConfirm(rs.getInt("seller_confirm"));
                record.setBuyerConfirm(rs.getInt("buyer_confirm"));
                list.add(record);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
}